<template>
	<div>
		<AplidForm ref="formRef" v-bind="ExtendFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { ExtendFormConfig } from '../../config'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { setTempParams,setLoading } = useCommonStore()
const { getValue, getContent } = useOperate(5)

type Props = {
	nowRowData?: any
}
const props = withDefaults(defineProps<Props>(), {})
let baseData: any = {
	dp_practitioners_experience: [],
	dp_training_record: [],
	dp_subsidy_info: [],
	dp_reward_punishment_info: [],
	dp_foundation_social_title: [],
	baseExtendData: {},
}

const emit = defineEmits(['submit'])
const formModel = ref({
	dp_practitioners_experience: [],
	dp_training_record: [],
	dp_subsidy_info: [],
	dp_reward_punishment_info: [],
  dp_foundation_social_title:[],
})
const formRef = ref(null)

const onSubmit = (form, done) => {
	const { tempNowObj } = storeToRefs(useCommonStore())
	// 从业经历
	const dpPractitionersExperiencePOList =
		form.dp_practitioners_experience &&
		form.dp_practitioners_experience.map((item) => {
			return {
				...item,
				city_id:undefined,
				idCard: tempNowObj.value.idCard,
				code: 2009,
			}
		})
	// 奖/处罚信息
	const dpRewardPunishmentInfoPOList =
		form.dp_reward_punishment_info &&
		form.dp_reward_punishment_info.map((item) => {
			return {
				...item,
				idCard: tempNowObj.value.idCard,
				code: 2012,
			}
		})
	// 补贴信息
	const dpSubsidyInfoPOList =
		form.dp_subsidy_info &&
		form.dp_subsidy_info.map((item) => {
			return {
				...item,
				idCard: tempNowObj.value.idCard,
				code: 2011,
			}
		})
	// 培训记录
	const dpTrainingRecordPOList =
		form.dp_training_record &&
		form.dp_training_record.map((item) => {
			return {
				...item,
				idCard: tempNowObj.value.idCard,
				code: 2010,
			}
		})

    // 社会职务
	const dpFoundationSocialTitlePOList =
		form.dp_foundation_social_title &&
		form.dp_foundation_social_title.map((item) => {
			return {
				...item,
				idCard: tempNowObj.value.idCard,
				code: 3037,
			}
		})

	const extendData = {
		...form,
		idCard: tempNowObj.value.idCard,
	}
	const expendBeforeData = baseData.baseExtendData || {}

	let changeData = []
	// 处理变更字段
	ExtendFormConfig.column.map((col:any) => {
		if (col.changeField) {
			const tempBefore = expendBeforeData[col.propName || col.prop]
			const tempAfter = extendData[col.propName || col.prop]
			if (tempBefore != tempAfter) {
				changeData.push({
					label: col.label,
					before: getValue(tempBefore, col),
					after: getValue(tempAfter, col),
				})
			}
		}
	})

	const emptyObj = {
		dp_practitioners_experience: undefined,
		dp_training_record: undefined,
		dp_subsidy_info: undefined,
		dp_reward_punishment_info: undefined,
		dp_foundation_social_title: undefined,
		code: undefined,
		idCard: tempNowObj.value.idCard,
	}

	const changeObj = {
		data: changeData,
		dataChangeContentList: [
			...getContent(baseData.dp_practitioners_experience, dpPractitionersExperiencePOList, 2009, '养老专家-从业经历', emptyObj, 'id'),
			...getContent(baseData.dp_training_record, dpTrainingRecordPOList, 2010, '养老专家-培训记录', emptyObj, 'id'),
			...getContent(baseData.dp_subsidy_info, dpSubsidyInfoPOList, 2011, '养老专家-补贴信息', emptyObj, 'id'),
			...getContent(baseData.dp_reward_punishment_info, dpRewardPunishmentInfoPOList, 2012, '养老专家-奖/处罚信息', emptyObj, 'id'),
			...getContent(baseData.dp_foundation_social_title, dpFoundationSocialTitlePOList, 3037, '养老专家-社会职务信息', emptyObj, 'id'),
			{
				changeBefore: {
					...expendBeforeData,
					...emptyObj,
				},
				changeAfter: {
					...extendData,
					...emptyObj,
				},
				changeData: {
					...extendData,
					...emptyObj,
					code: 2008,
				},
				changeType: '从业人员扩展信息',
				code: 2008,
			},
		],
	}
	setTempParams({
		noChange: props.nowRowData.workType == 2,
		name: tempNowObj.value.name,
		idCard: tempNowObj.value.idCard,
		nowChange: changeObj,
	})
	emit('submit', changeObj)
	done()
}

const getInitData = () => {
	const query = { idCard: props.nowRowData.idCard }
  setLoading()
	Promise.all([
		apiMod.view_dpPractitionersExperience_page(query),
		apiMod.view_dpTrainingRecord_page(query),
		apiMod.view_dpSubsidyInfo_page(query),
		apiMod.view_dpRewardPunishmentInfo_page(query),
		apiMod.view_dpFoundationPractitionersExtend_page(query),
		apiMod.view_dpFoundationSocialTitle_page(query),
	])
		.then((res) => {
			return res.map((item) => item.data.records)
		})
		.then((res) => {
			const extendObj = res[4][0] || {}
      setLoading()
	  		res[0].city_id = res[0].city
			formModel.value = {
				dp_practitioners_experience: res[0],
				dp_training_record: res[1],
				dp_subsidy_info: res[2],
				dp_reward_punishment_info: res[3],
        dp_foundation_social_title: res[5],
				...extendObj,
			}
			baseData = Object.freeze(
				JSON.parse(
					JSON.stringify({
						dp_practitioners_experience: res[0],
						dp_training_record: res[1],
						dp_subsidy_info: res[2],
						dp_reward_punishment_info: res[3],
						dp_foundation_social_title: res[5],
						baseExtendData: extendObj,
					})
				)
			)

			console.log(res, 'res---')
		})
}

onMounted(() => {
	getInitData()
})

const onSave = () => {
	formRef.value!.onSubmit()
}
defineExpose({
	onSave,
})
</script>

<style scoped></style>
